﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.OA;
using SmartSpace.Web;

namespace SmartSpace.WebApp.Modules.OA.Pages
{
    public partial class Asset_List : SsTemplatePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string orgId = Request.QueryString["orgId"] ?? string.Empty;
            orgId = "{DE385795-CC31-4A3F-A346-71EF6B6037DD}";
            if (!IsPostBack)
            {
                BindAsset();
            }

            if (orgId == "")
            {
                AddAsset.Enabled = false;
            }
            else {
                AddAsset.Enabled = true;
                AddAsset.NavigateUrl = "Asset-Add.aspx?orgId=" + orgId;
            }
        }

        public void BindOrgTree()
        { 
            
        }

        public void BindAsset()
        {
            string orgId = Request.QueryString["orgId"] ?? string.Empty;
            if (orgId == "")
            {
                orgId = Guid.Empty.ToString();
            }
            OAService service = new OAService();
            PagedList<Asset> list = service.GetAsset("", new Guid(orgId), 1, 9999);
            rep1.DataSource = list;
            rep1.DataBind();
        }

        protected void rep1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string orgId = Request.QueryString["orgId"] ?? string.Empty;
            OAService service = new OAService();
            string assetId = e.CommandArgument.ToString();
            if (e.CommandName == "Delete")
            {
                ServiceResult result = service.DeleteAsset(new Guid(assetId));
                if (result.IsSuccess)
                {
                    Response.Redirect("Asset-List.aspx?orgId=" + orgId);
                }
            }
        }
    }
}